<?php

namespace app\model;

use app\stateData;
use think\facade\Db;
use think\facade\Log;
use think\Model;

class CluePerson extends Model
{
    /**
     * 线索库人才库
     */
    protected $createTime = 'create_date';


    //关联联系电话
    public function communicateRecord()
    {
        return $this->hasOne(ClueCommunicateRecord::class,'clue_p_id','id')->bind (['next_communicate_date','new_date'=>'create_date']);
    }

    public function getIds()
    {
//        $list = self::field('id')->select()->toArray();
//        return array_column($list, 'id');
        return $this->column ('id');
    }

    public function getCertificateBAttr($name,$data)
    {
        if ($name === ''){
            return [
                'id'=>'',
                'text'=>''
            ];
        }
        return [
            'id'=>(int)$name,
            'text'=>stateData::$_b_doc[$name]?:''
        ];
    }

    public function getFUidAttr($name)
    {
        return [
            'id' => $name,
            'text' => (new User)->getIdNickName($name)?:''
        ];
    }

    public function getSocialSecurityAttr($name)
    {
        if ($name === ''){
            return [
                'id'=>'',
                'text'=>''
            ];
        }
        return [
            'id'=>(int)$name,
            'text'=>stateData::$_social_security[$name]?:''
        ];
    }

    public function getProjectAttr($name)
    {
        if ($name === ''){
            return [
                'id'=>'',
                'text'=>''
            ];
        }
        return [
            'id'=>(int)$name,
            'text'=>stateData::$_project[$name]?:''
        ];
    }

    public function getIsWxAttr($name)
    {
        return [
            'id'=>$name,
            'text'=>stateData::$_is_wx[$name]?:''
        ];
    }

    public function getIsFAttr($name)
    {
        return [
            'id'=>$name,
            'text'=>stateData::$_is_f[$name]?:''
        ];
    }

    public function getCustomAttr($name)
    {
        return json_decode($name, false);
    }

    public function getCertificatePeriodOfValidityAttr($name)
    {
        if ($name === "0000-00-00 00:00:00") {
            return "";
        }
        return date('Y-m-d',strtotime($name));
    }

    public function getCertificateBPeriodOfValidityAttr($name)
    {
        if ($name === "0000-00-00 00:00:00") {
            return "";
        }
        return date('Y-m-d',strtotime($name));
    }

    public function getCertificateLeixingAttr($name)
    {
        if ($name == 0) {
            return "";
        }
        return (new SystemCertificate)->getWhereIdCertificateModel($name) ?: '';
    }

    public function getCertificateLeibieAttr($name)
    {
        if ($name == 0) {
            return "";
        }
        return (new SystemCertificate)->getWhereIdCertificateModel($name) ?: '';
    }

    public function getCertificateDengjiAttr($name)
    {
        if ($name == 0) {
            return "";
        }
        return (new SystemCertificate)->getWhereIdCertificateModel($name) ?: '';
    }

    public function getCertificateZhuanyeAttr($name)
    {
        if ($name == 0) {
            return "";
        }
        return (new SystemCertificate)->getWhereIdCertificateModel($name) ?: '';
    }

    public function getCertificateLogOffAttr($name)
    {
        if (empty($name) || $name == '0000-00-00 00:00:00') return "";
        return date('Y-m-d',strtotime($name));
    }

    public function getAllocationDateAttr($name)
    {
        if (empty($name) || $name == '0000-00-00 00:00:00') return "";
        return $name;
    }
    public function getBecomeDueAttr($name)
    {
        if (empty($name) || $name == '0000-00-00 00:00:00') return "";
        return date('Y-m-d',strtotime($name));
    }


    /**
     * 导入设置
     * @param $name
     * @return false|int|string
     */

    public function setCertificateIdAttr($name)
    {
        if (preg_match("/[\x7f-\xff]/", $name)) {
            $name = (new SystemCertificate())->getWhereCertificateIdModel($name);
        }
        return $name?:'';
    }

    public function setCertificateBAttr($name)
    {
        if (preg_match("/[\x7f-\xff]/", $name)) {
            $name = array_search($name, stateData::$_b_doc, true);
        }
        return $name?:'';
    }

    public function setSocialSecurityAttr($name)
    {
        if (preg_match("/[\x7f-\xff]/", $name)) {
            $name = array_search($name, stateData::$_social_security, true);
        }
        return $name?:'';
    }

    public function setProjectAttr($name)
    {
        if (preg_match("/[\x7f-\xff]/", $name)) {
            $name = array_search($name, stateData::$_project, true);
        }
        return $name?:'';
    }

    public function setIsWxAttr($name)
    {
        if (preg_match("/[\x7f-\xff]/", $name)) {
            $name = array_search($name, stateData::$_is_wx, true);
        }
        return $name?:'';
    }

    public function setIsFAttr($name)
    {
        if (preg_match("/[\x7f-\xff]/", $name)) {
            $name = array_search($name, stateData::$_is_f, true);
        }
        return $name?:'';
    }

    public function getZdDateAttr($name)
    {
        if (empty($name) || $name=='0000-00-00 00:00:00') return "";
        return date ('Y-m-d',strtotime ($name));
    }


    /**
     * 获取人才信息
     * @return CluePerson[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonListModel($page = 0)
    {
        return $this->page($page, stateData::$_page)->select();
    }

    /**
     * 添加人才信息
     * @param $data
     * @return CluePerson|Model
     */
    public function addPersonModel($data)
    {
        return self::create($data);
    }

    /**
     * 修改人才基本信息
     * @param $data
     * @return \app\model\CluePerson|bool
     */
    public function editPersonModel($data)
    {
        return self::update($data);
    }
    public function editPersonIdModel($id,$data)
    {
        return $this->where(['id'=>$id])->update ($data);
    }

    /**
     * 查询该身份证是否已经录入
     * @param $idCard
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonIdCard($idCard): bool
    {
        return (bool)$this->where(['person_id_card' => $idCard])->find();
    }

    /**
     * 获取人才基本信息
     * @param $id
     * @return array|null
     */
    public function getIdPersonInfo($id)
    {
        $data = $this->where(['id' => $id, 'is_del' => 0])->findOrEmpty();
        if (!$data->isEmpty()) {
            return $data->toArray();
        }

        return null;
    }

    public function redisGetIdPersonInfo($id)
    {
        $data = $this->where(['id' => $id])->findOrEmpty();
        if (!$data->isEmpty()) {
            return $data->toArray();
        }

        return null;
    }

    public function getIdPersonInfoModel($id)
    {
        $data = $this->where(['id' => $id])->findOrEmpty();
        if (!$data->isEmpty()) {
            return $data->toArray();
        }

        return null;
    }

    /**
     * @param $idcard
     * @param $uid
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonIdCardFuid($idcard, $uid): bool
    {
        return $this->where(['person_id_card' => $idcard, 'f_uid' => $uid, 'is_del' => 0])->find();
    }

    /**
     * 通过身份证查询人才信息
     * @param $idcard
     * @return \app\model\CluePerson|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonIdCardInfo($idcard)
    {
        return $this->where(['person_id_card' => $idcard])->find();
    }

    /**
     * 管理查询批量Fid对应的人才信息
     * @param $idArr
     * @param $where
     * @param int $page
     * @return \app\model\CluePerson[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRangeData($idArr, $pageNum, int $page = 1)
    {
        $idArr[] = 0;
        return $this->where(['is_del' => 0])->whereIn('f_uid', $idArr)->page($page, $pageNum)->select();
    }

    public function newGetRangeData($idArr, $pageNum, $person_ids, int $page = 1)
    {
//        $idArr[] = 0;
//        return $this->where(['is_del' => 0])->whereIn('f_uid', $idArr)->where('id', 'in', $person_ids)->order('create_date DESC')->page($page, $pageNum)->select();
        return $this->where(['is_del' => 0])
//            ->whereIn('f_uid', $idArr)
//                ->fetchSql(true)
            ->where('id', 'in', $person_ids)
            ->order(['allocation_date'=>'DESC','create_date'=>'DESC'])
//            ->page($page, $pageNum)
            ->column('id');
    }

    /**
     * 一键分配列表
     * @param $person_ids
     * @return array
     */
    public function newIdDataModel($person_ids)
    {
//        $idArr[] = 0;
//        return $this->where(['is_del' => 0])->whereIn('f_uid', $idArr)->where('id', 'in', $person_ids)->order('create_date DESC')->page($page, $pageNum)->select();
        return $this->where(['is_del' => 0])
//            ->whereIn('f_uid', $idArr)
            ->where('id', 'in', $person_ids)
            ->order(['allocation_date'=>'DESC','create_date'=>'DESC'])
            ->column('id');
    }
    public function newGetRangeAllData($idArr, $person_ids)
    {
        $idArr[] = 0;
        return $this->where(['is_del' => 0])->where('id', 'in', $person_ids)->order('create_date DESC')->column ('id');
    }

    public function newGetRangeDataPersonId($idArr,$data,$param='')
    {
        $idArr[] = 0;
        $person_list = $this->where(['is_del' => 0]);
        if (!$param['uid'] && !$param['org_id']){
            $person_list->where('is_f',1);
        }

        if(!$data['rcgh']){
            $person_list->where('is_f',2);
            $person_list->whereIn('f_uid', $idArr);
        }
        return $person_list->column ('id');
//        return array_column($person_list, 'id');
    }

    public function getRangeDataCount($idArr, $where)
    {
        $idArr[] = 0;
        return $this->where(['is_del' => 0])->whereIn('f_uid', $idArr)->where($where)->count();
    }

    public function newGetRangeDataCount($idArr, $person_ids)
    {
        $idArr[] = 0;
        return $this->where(['is_del' => 0])->whereIn('f_uid', $idArr)->where('id', 'in', $person_ids)->count();
    }

    /**
     * 个人查询批量Fid对应的人才信息
     * @param $idArr
     * @param $where
     * @param int $page
     * @return \app\model\CluePerson[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUidData($idArr, $ids, $pageNum,int $page = 1)
    {

//        return self::fetchSql(true)->where(['is_del' => 0])->whereIn('f_uid', $idArr)->where('id', 'in', $ids)->order('create_date DESC')->order('id DESC')->select();
//        return $this->where(['is_del' => 0])->whereIn('f_uid', $idArr)->where('id', 'in', $ids)->order('create_date DESC')->order('id DESC')->page($page, $pageNum)->select()->toArray();
        return $this->where(['is_del' => 0])
//            ->fetchSql(true)
//            ->whereIn('f_uid', $idArr)
            ->where('id', 'in', $ids)
            ->order(['allocation_date'=>'DESC'])
//            ->order('allocation_date DESC')
            ->page($page, $pageNum)
            ->column ('id');
    }

    /**
     * 排序ID集合
     * @param $ids
     * @return array
     */
    public function getOrderIdResultModel( $ids)
    {
        $result = [];
        $i=1;
        $pageNum = 50;
        while (true){
            $data = $this->where(['is_del' => 0])
//                ->fetchSql(true)
                ->where('id', 'in', $ids)
                ->order(['allocation_date'=>'DESC'])
                ->page($i, $pageNum)
                ->column ('id');
            if (empty($data)){
                break;
            }
            $result = array_merge ($result,$data);
            $i++;
        }

        return $result;
    }
    public function getUidDataPersonId($idArr)
    {
//        $list = $this->where(['is_del' => 0])->whereIn('f_uid', $idArr)->field('id')->select()->toArray();
//        return array_column($list, 'id');
        return $this->where(['is_del' => 0])->whereIn('f_uid', $idArr)->column ('id');
    }

    public function getUidDataCount($idArr, $ids)
    {
        return $this->where(['is_del' => 0])->whereIn('f_uid', $idArr)->where('id', 'in', $ids)->count();
    }

    /**
     *
     * 查询回收站人员
     * @return \app\model\CluePerson[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDelEqRecycle($page, $pageNum, $ids)
    {
        return $this->where('is_del', '=', '1')->where('id', 'in', $ids)->order('create_date DESC')->page($page, $pageNum)->select()->toArray();
    }
    public function getDelEqRecycleAllModel($ids)
    {
//        return $this->where('is_del', '=', '1')->where('id', 'in', $ids)->order('create_date DESC')->select()->toArray();
        return $this->where('is_del', '=', '1')->where('id', 'in', $ids)->order('create_date DESC')->column ('id');
    }

    public function getDelEqRecycleIds()
    {
        $list = $this->where('is_del', '=', '1')->field('id')->select()->toArray();
        return array_column($list, 'id');
    }

    public function getDelEqRecycleCount($ids)
    {
        return $this->where('is_del', '=', '1')->where('id', 'in', $ids)->count();
    }

    /**
     * 查询所有人才列表
     * @return \app\model\CluePerson[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonList($uid, $page, $pageNum, $ids)
    {
        return $this->where('id', 'in', $ids)->column('id');
    }

    public function getPersonIds($uid)
    {
//        $list = $this->whereIn('f_uid', $uid)->field('id')->select()->toArray();
//        return array_column($list, 'id');
        return $this->whereIn('f_uid', $uid)->field('id')->column ('id');
    }

    /**
     *
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getPersonCount($ids): int
    {
        return $this->where('id', 'in', $ids)->count();
    }

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function delId($id)
    {
        return self::destroy($id);
    }

    /**
     * 批量添加人才数据
     * @param $param
     * @return \think\Collection
     * @throws \Exception
     */
    public function addAllDataModel($param)
    {
        return $this->saveAll($param);
    }

    /**
     * 自动调回
     * @param $where
     * @return \app\model\CluePerson
     */
    public function autoWhereDhExe($where)
    {
        return $this->where($where)->update(['is_f' => 0, 'f_uid' => 0]);
    }


    /**
     * 自动回收
     * @param $where
     * @return \app\model\CluePerson
     */
    public function autoWhereHsExe($where)
    {
        return $this->where($where)->update(['is_f' => 0, 'f_uid' => 0, 'is_del' => 1]);
    }

    /**
     * 查询数据范围内容
     * @param $idArr
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIdRandData($idArr): array
    {
//        return $this->whereIn('id', $idArr)->order('create_date DESC')->select()->toArray();
        return $this->whereIn('id', $idArr)->order('create_date DESC')->column ('id');
    }

    /**
     * 修改人才状态为续签
     * @param $idcard
     * @return \app\model\CluePerson
     */
    public function setIdCardStatusModel($idcard)
    {
        return $this->where(['person_id_card' => $idcard])->update(['status' => 6]);
    }

    /**
     * 修改人才状态为代收证
     * @param $idcard
     * @return \app\model\CluePerson
     */
    public function setIdCardStatusSzModel($idcard)
    {
        return $this->where(['person_id_card' => $idcard])->update(['status' => 1]);
    }

    public function getPersonDataModel()
    {
        return $this->select()->toArray ();
    }

    /**
     * 设置重点客户
     * @param $id
     * @return \app\model\CluePerson
     */
    public function setZDModel($id)
    {
        return $this->where (['id'=>$id])->update (['is_zd'=>2,'zd_date'=>date('Y-m-d H:i:s')]);
    }

    public function cancellationZDModel($id){
        return $this->where (['id'=>$id])->update (['is_zd'=>1,'zd_date'=>'']);
    }

    /**
     * 获取最大的ID
     * @return mixed
     */
    public function getMaxIdModel()
    {
        return $this->where (['is_del'=>0])->max('id');
    }

    /**
     * 修改人才状态
     * @param $id
     * @param $status
     * @return \app\model\CluePerson
     */
    public function editStatusModel($id,$status)
    {
        return $this->where (['person_id_card'=>$id])->update (['status'=>$status]);
    }

    /**
     * 查询所有分配给uid的人才信息id
     * @param $uid
     * @return array
     */
    public function getFUidDataModel($uid): array
    {
        return $this->where (['f_uid'=>$uid])->column('id');
    }

    /**
     * 修改人才信息id集合分配人
     * @param $idArr
     * @param $fuid
     * @return \app\model\CluePerson
     */
    public function editHandOverFUidModel($idArr,$fuid)
    {
        return $this->whereIn ('id',$idArr)->update (['f_uid'=>$fuid]);
    }

    public function getFUidFieldDataModel($uid)
    {
        return $this->where (['f_uid'=>$uid])->field('id,person_name as title,person_id_card,f_uid')->select ()->toArray ();
    }

    public function setIdCardStatus($person_id_card,$status =3)
    {
        return $this->where(['person_id_card' => $person_id_card])->update(['status' => $status]);
    }

    public function setIdCardDcsStatus($person_id_card,$status =2)
    {
        return $this->where(['person_id_card' => $person_id_card])->update(['status' => $status]);
    }

    /**
     * 通过人才姓名查询
     * @param $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonNameDataModel($name,$uid)
    {
//        return $this->where (['person_name'=>$name,'is_del'=>0])->where(['f_uid'=>$uid])->select ()->toArray ();
        return $this->where (['person_name'=>$name,'is_del'=>0])->select ()->toArray ();
    }

    /**
     * 获取所有人才姓名
     * @return array
     */
    public function getAllPersonNameDataModel($name)
    {
        return $this->whereLike('person_name','%'.$name.'%')->field('person_name as title,id')->select ()->toArray ();
    }
}